import QtQuick 1.1
import Qt.labs.particles 1.0

Item {
    id: ball
    property bool dying: false
    property bool spawned: false
    property int type: 0
    property bool selected: false
    property bool moving: false
    property real nextX: 0
    property real nextY: 0
    signal clicked(real x, real y)
    signal movingFinished

    PropertyAnimation {
        running: selected
        loops: Animation.Infinite
        target: img
        property: "opacity"
        easing.type: Easing.InOutCubic
        from: 0.5
        to: 1
        duration: 200
    }

    SequentialAnimation {
        running: ball.moving
        ParallelAnimation {
            NumberAnimation {
                target: ball;
                property: "x";
                to: nextX;
                duration: 80
            }
            NumberAnimation {
                target: ball;
                property: "y";
                to: nextY;
                duration: 80
            }
        }
        ScriptAction {script: movingFinished()}
    }


    SequentialAnimation {
        NumberAnimation { duration: 50 }
        ScriptAction {script: movingFinished()}
    }


    Image {
        id: img
        smooth: true
        width: parent.width
        height: parent.height
        anchors.centerIn: parent
        source: {
//            if (type == 0) {
//                "../../images/Orb_Icons_004.png";
//            } else if (type == 1) {
//                "../../images/Orb_Icons_008.png";
//            } else if (type == 2) {
//                "../../images/Orb_Icons_015.png";
//            } else if (type == 3){
//                "../../images/Orb_Icons_016.png";
//            } else if (type == 4){
//                "../../images/Orb_Icons_019.png";
//            } else if (type == 5){
//                "../../images/Orb_Icons_024.png";
//            } else if (type == 6){
//                "../../images/Orb_Icons_025.png";
//            } /*else if (type == 7) {
//                "../../images/Orb_Icons_016.png";
//            } else if (type == 8) {
//                "../../images/Orb_Icons_020.png";
//            }*/
            if (type == 0) {
                "../../images/blackStone.png";
            } else if (type == 1) {
                "../../images/blueStone.png";
            } else if (type == 2) {
                "../../images/greenStone.png";
            } else if (type == 3){
                "../../images/orangeStone.png";
            } else if (type == 4){
                "../../images/redStone.png";
            } else if (type == 5){
                "../../images/violetStone.png";
            } else if (type == 6){
                "../../images/yellowStone.png";
            }
//            else if (type == 7) {
//                "../../images/Orb_Icons_016.png";
//            } else if (type == 8) {
//                "../../images/Orb_Icons_020.png";
//            }
//            if (type == 0) {
//                "../../images/aqua.png";
//            } else if (type == 1) {
//                "../../images/blue.png";
//            } else if (type == 2) {
//                "../../images/green.png";
//            } else if (type == 3){
//                "../../images/red.png";
//            } else if (type == 4){
//                "../../images/violet.png";
//            } else if (type == 5){
//                "../../images/yellow.png";
//            } else if (type == 6){
//                "../../images/black.png";
//            }
        }
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            ball.clicked(ball.x, ball.y)
        }
    }

    Particles {
        id: particles

        width: 1; height: 1
        anchors.centerIn: parent

        emissionRate: 0
        lifeSpan: 700; lifeSpanDeviation: 600
        angle: 0; angleDeviation: 360;
        velocity: 100; velocityDeviation: 30
        source: {
            if (type == 0) {
                "../../images/blackStar.png";
            } else if (type == 1) {
                "../../images/blueStar.png";
            } else if (type == 2) {
                "../../images/greenStar.png";
            } else if (type == 3){
                "../../images/orangeStar.png";
            } else if (type == 4){
                "../../images/redStar.png";
            } else if (type == 5){
                "../../images/violetStar.png";
            } else if (type == 6){
                "../../images/yellowStar.png";
            }
        }
    }

    states: [
        State {
            name: "NormalState";
            when: selected != true && spawned == true && dying == false
            PropertyChanges { target: img; opacity: 1 }
        },

        State {
            name: "SelectedState"
            when: selected == true && spawned == true && dying == false
        },

        State {
            name: "DeathState";
            when: dying == true
            StateChangeScript { script: particles.burst(50); }
            PropertyChanges { target: img; opacity: 0 }
            StateChangeScript { script: ball.destroy(1000); }
        }
    ]
}
